package com.st.thrift;

import com.st.spiderfw.thrift.SpiderThrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;


public class ThriftDemo {

  public static void main(String arg[]) throws Exception {
    String keyId = "1001";
    String jarFileName = "baidu.jar";
    String className = "com.spidertemplate.extract.baidu.SearchNews";
    String openUrl = "http://news.baidu.com/ns?word=%E5%8D%97%E7%BE%8E%E8%B6%85%E7%BA%A7%E5%BE%B7%E6%AF%94%E6%9D%AF%E3%80%80oppo&pn=0&cl=2&ct=1&tn=news&rn=20&ie=utf-8&bt=0&et=0";
    String requestParameter = "{ \"cookie\":{},"
            + "\"header\":{},"
            + "\"charset\":\"utf-8\","
            + "\"httpProxyIP\":\"\","
            + "\"gzip\":\"true\","
            + "\"requestMethod\":\"get\","
            + "\"postParams\":{}}";
    String msg = new ThriftDemo().sendSpiderFW(keyId, jarFileName, className, openUrl, requestParameter);
    System.out.println(msg);
  }

  public String sendSpiderFW(String keyId, String jarFileName, String className, String openUrl, String requestParameter) throws Exception {

    try (TTransport transport = new TSocket("172.16.0.91", 10020, 30000)) {
      transport.open();
//    transport.isOpen();
      TProtocol protocol = new TBinaryProtocol(transport);

      SpiderThrift.Client client = new SpiderThrift.Client(protocol);
      String taskId = client.sendSpiderFW(keyId, jarFileName, className, openUrl, requestParameter, "high");

//      transport.close();
      return taskId;
    } catch (Exception e) {
      e.printStackTrace();
      throw new Exception(e.getMessage());
    }


  }


}
